<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use App\Models\User;
use App\Models\Brand;
use App\Models\HwCity;
use Brick\Math\RoundingMode;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
 


class Test extends Command
{
    protected $signature = 'cc:test';

    protected $description = "";

    public function handle()
    {

        Brand::truncate();
        
       $response = Http::get('https://jsapi.58moto.com/carport/seo/brand/rank/list?rows=500&platform=6&version=3.41.5');
       if ($response->successful()) {
            $data = $response->json();
            $insert = [];
            foreach($data['data'] as $k=>$v){

                $logo = $v['brandLogo'];
                $imageContent = file_get_contents($logo);

                $newFileName = "motuo/".Str::random(40) . '.jpg'; // 假设是JPG格式

                Storage::disk('public')->put($newFileName, $imageContent);
                $insert[$k] = [

                    'id'=>$v['brandId'],
                    'name' => $v['brandName'],
                    'image'=>$newFileName,
                    'created_at'=>Carbon::now(),
                ];

               

            }

             Brand::insert($insert);

            // 处理数据
        } else {
            // 错误处理
        }


    }
}
